package org.erepublik.company.scanner;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.bind.JAXBException;

import org.erepublik.company.ECountry;
import org.erepublik.company.Hospital;
import org.erepublik.company.Region;
import org.erepublik.company.generated.HospitalT;
import org.erepublik.company.generated.HospitalsT;
import org.erepublik.company.generated.RegionTTT;
import org.erepublik.company.generated.RegionsTT;
import org.erepublik.company.utils.WebpageReader;
import org.erepublik.company.xml.XMLParsersers;
import org.json.JSONArray;
import org.json.JSONException;

public class HospitalsScanner {

    
    
    
    public HospitalsScanner() {

    }
    
    public ArrayList<Hospital> scannHospitals() {
        ArrayList<Hospital> regionList = new ArrayList<Hospital>();
        for (ECountry country : ECountry.values()) {
            regionList.addAll(scannHospitalsFromCountry(country));
        }
        return regionList;
    }
    
    
    private ArrayList<Hospital> scannHospitalsFromCountry(ECountry country) {
        System.out.println("Scanning hospitals from " + country);
        InputStream stream = null;
        RegionsTT products = null;
        try {
            stream = WebpageReader.getStreamToIrl("http://api.erepublik.com/v2/feeds/countries/"+ country.getErepublikId() +"/regions");
            products = XMLParsersers.parseRegions(stream);
        } catch (IOException e) {
            e.printStackTrace();
            return new ArrayList<Hospital>();
        } catch (JAXBException e) {
            e.printStackTrace();
            return new ArrayList<Hospital>();
        }
        
        return getHospitalsFromJaxb(products);
    }

    
    private ArrayList<Hospital> getHospitalsFromJaxb(RegionsTT regions) {     
        ArrayList<Hospital> regionList = new ArrayList<Hospital>();
        for (RegionTTT region : regions.getRegion()) {     
            HospitalsT hospitals = region.getBuildings().getHospitals();
            for (HospitalT hospitalT : hospitals.getHospital()) {
                Hospital hospital = new Hospital();
                hospital.setRegionName( region.getName() );
                hospital.setRegion( hospitalT.getZoneId() );
                hospital.setWellness( hospitalT.getWellnessBudget() );

                hospital.setWellnessPerPlayer( hospitalT.getMaxHealPerCitizen()*10 );
                regionList.add(hospital);
            }
        }
        return regionList;
    }

  
    
    
    public static void main(String[] args) {
        RegionsScanner scanner = new RegionsScanner();
        ArrayList<Region> regions = scanner.scannRegions();
        for (Region region : regions) {
            System.out.println(region.getName() + " " + region.getCountry());
            System.out.println("grain: "+region.getGrain());
            System.out.println("iron: "+region.getIron());
            System.out.println("oil: "+region.getOil());
            System.out.println("stone: "+region.getStone());
            System.out.println("titanium: "+region.getTitanium());
            System.out.println("");
        }
        try {
            saveRegionsToJSON(regions, "src/main/resources/regions.json");
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

    
    
    private static void saveRegionsToJSON(ArrayList<Region> regions, String path) throws JSONException, IOException {
        JSONArray jsonArray = new JSONArray();
        for (Region region : regions) {
            jsonArray.put(region.toJSONObject());
        }
        BufferedWriter out = new BufferedWriter(new FileWriter(new File(path)));
        out.write(jsonArray.toString());
        out.close();
        
    }
    
    
    
    
    
}
